﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NPSH_Check
{
    public partial class Form1 : Form
    {
        Microsoft.Office.Interop.Excel.Application oXL;
        Microsoft.Office.Interop.Excel._Workbook oWB;
        Microsoft.Office.Interop.Excel._Worksheet oSheet;
        Microsoft.Office.Interop.Excel.Range oRng;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double.TryParse(txtLs.Text, out UserInputs.Ls);
            double.TryParse(txtHvs.Text, out UserInputs.Vs);
            double.TryParse(txtBs.Text, out UserInputs.Bs);
            double.TryParse(txtRs.Text, out UserInputs.Rs);
            double.TryParse(txtHes.Text, out UserInputs.Hes);
            double.TryParse(txtTemp.Text, out UserInputs.Temp);
            double.TryParse(txtEle.Text, out UserInputs.Ele);
            double.TryParse(txtSG.Text, out UserInputs.SG);
            double.TryParse(txtTemps.Text, out UserInputs.Temps);
            double.TryParse(txtNPSHa.Text, out UserInputs.Q);
            double.TryParse(txtQ.Text, out UserInputs.Q);
            double.TryParse(txtDsi.Text, out UserInputs.Dsi);
            double.TryParse(txtffm.Text, out UserInputs.f);
            double.TryParse(txtNPSHr.Text, out UserInputs.NPSHr);
            double.TryParse(txtµ.Text, out UserInputs.µ);
            UserInputs.RoughnessCoefficientChoice = (ERoughnessCoefficientType)cboRoughnessCoefficient.SelectedIndex;
            double.TryParse(txtDnominal.Text, out UserInputs.Dnominal);
            double.TryParse(txtHed.Text, out UserInputs.Hed);
            UserInputs.KinletChoice = (EKinletType)cboKinlet.SelectedIndex;
            UserInputs.NominalDiameterChoice = (ENominalDiameterType)cboNominalDiameter.SelectedIndex;
            UserInputs.PipeFittingChoice = (EPipeFittingType)cboPipeFitting.SelectedIndex;

            Calc npshcalc = new Calc();
            Outputs output = npshcalc.Start();
            txtHatm.Text = (output.Hatm * 102 / 1000000).ToString("0.00");
            txtHv.Text = (output.Hv).ToString("0.00");
            txtNPSHa.Text = (output.NPSHa).ToString("0.00");
            txtMessage.Text = output.Message;
            txtρm.Text = (output.ρm).ToString("0");
            txtRn.Text = (output.Rn).ToString("0");
            txtVsi.Text = (output.Vsi).ToString("0.00");
            txtMessage1.Text = output.Message1;
            txtff.Text = (output.ff).ToString("0.0000");
            txtHf.Text = (output.Hf).ToString("0.00");
            txtHvs.Text = (output.Hvs).ToString("0.000");
            txtHinlet.Text = (output.Hinlet).ToString("0.000");
        }

        private void label10_Click(object sender, EventArgs e)
        {
        }

        private void textBox1_TextChanged_2(object sender, EventArgs e)
        {
        }

        private void label22_Click(object sender, EventArgs e)
        {
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cboRoughnessCoefficient.Items.AddRange(StaticHelper.GetEnumDescriptions<ERoughnessCoefficientType>());
            cboRoughnessCoefficient.SelectedIndex = (int)ERoughnessCoefficientType.FeSteel;

            cboKinlet.Items.AddRange(StaticHelper.GetEnumDescriptions<EKinletType>());
            cboKinlet.SelectedIndex = (int)EKinletType.SharpEdgedrOverd0;

            cboNominalDiameter.Items.AddRange(StaticHelper.GetEnumDescriptions<ENominalDiameterType>());
            cboNominalDiameter.SelectedIndex = (int)ENominalDiameterType.Fifteen;

            cboPipeFitting.Items.AddRange(StaticHelper.GetEnumDescriptions<EPipeFittingType>());
            cboPipeFitting.SelectedIndex = (int)EPipeFittingType.GTV;
        }

        private void txtVs_TextChanged(object sender, EventArgs e)
        {
        }

        private void label2_Click(object sender, EventArgs e)
        {
        }

        private void txtDsi_TextChanged(object sender, EventArgs e)
        {
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int row, col;
            row = (int)((EPipeFittingType)cboPipeFitting.SelectedIndex) + 2;
            col = (int)((ENominalDiameterType)cboNominalDiameter.SelectedIndex) + 2;
            oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[row, col];
            txtKfactor.Text = oRng.Text.ToString();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            //Start Excel and get Application object.
            oXL = new Microsoft.Office.Interop.Excel.Application();
            oXL.Visible = true;
            oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Open(Application.StartupPath + @"\PipeFittings.xlsx"));
            oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                oWB.Close(false);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }
    }
}